﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.ApplicationPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

namespace CrossSiteCollectionSearchConfigurator
{
    [CLSCompliant(false)]
    public class ManageSearchOptions : Microsoft.SharePoint.ApplicationPages.ApplicationsManagementPage
    {
        protected Button BtnSubmit;
        protected WebApplicationSelector Selector;
        protected RadioButtonList MasterSiteCollectionSelector;
        protected CheckBoxList SlaveSiteCollectionsSelector;

        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            SPWebApplication selectedWebApp = Selector.CurrentItem;
            string selectedMasterUrl = MasterSiteCollectionSelector.SelectedValue;
            List<string> selectedSlaveUrls = new List<string>();
            foreach (ListItem slaveItem in SlaveSiteCollectionsSelector.Items)
            {
                if (slaveItem.Selected)
                {
                    selectedSlaveUrls.Add(slaveItem.Value);
                }
            }
            //Itterate through the site collections of the selected WebApp and set master and slave staus as needed.
            foreach (SPSite siteCollection in selectedWebApp.Sites)
            {

                SPWeb rootWeb = siteCollection.RootWeb;
                
                //clear any previous entries of Master
                bool isMaster = Utilities.CheckIsMaster(rootWeb);
                if (isMaster)
                {
                    rootWeb.Properties[Constants.CS2C_IS_MASTER_KEY] = Constants.FALSE_STRING;
                }
                bool isSlave = Utilities.CheckIsSlave(rootWeb);
                if (isSlave)
                {
                    rootWeb.Properties[Constants.CS2C_IS_SLAVE_KEY] = Constants.FALSE_STRING;
                }

                if (siteCollection.Url == selectedMasterUrl)
                {
                    //Set site as Master
                    if (null == rootWeb.Properties[Constants.CS2C_IS_MASTER_KEY])
                    {
                        rootWeb.Properties.Add(Constants.CS2C_IS_MASTER_KEY, Constants.TRUE_STRING);
                    }
                    else 
                    {
                        rootWeb.Properties[Constants.CS2C_IS_MASTER_KEY] = Constants.TRUE_STRING;
                    }
                }
                else if (selectedSlaveUrls.Contains(siteCollection.Url))
                {
                    //Set the collection as a slave.
                    if (null == rootWeb.Properties[Constants.CS2C_IS_SLAVE_KEY])
                    {
                        rootWeb.Properties.Add(Constants.CS2C_IS_SLAVE_KEY, Constants.TRUE_STRING);
                    }
                    else
                    {
                        rootWeb.Properties[Constants.CS2C_IS_SLAVE_KEY] = Constants.TRUE_STRING;
                    }
                }

                rootWeb.Properties.Update();

                rootWeb.Dispose();
                siteCollection.Dispose();
            }
            OnContextChange(null, null);
        }

        public void OnContextChange(object sender, EventArgs e)
        {

            BindMasterSiteCollectionSelector();
            BindSlaveSiteCollectionSelector();
        }

        private void BindMasterSiteCollectionSelector()
        {
            if (null != Selector.CurrentItem)
            {
                SPWebApplication selectedWebApp = Selector.CurrentItem;
                MasterSiteCollectionSelector.Items.Clear();
                foreach (SPSite siteCollection in selectedWebApp.Sites)
                {
                    ListItem option = new ListItem();
                    MasterSiteCollectionSelector.Items.Add(option);
                    option.Text = siteCollection.Url;
                    option.Value = option.Text;

                    SPWeb rootWeb = siteCollection.RootWeb;
                    bool isMaster = Utilities.CheckIsMaster(rootWeb);
                    if (isMaster)
                    {
                        option.Selected = true;
                    }
                    rootWeb.Dispose();
                    siteCollection.Dispose();
                }
            }
        }

        private void BindSlaveSiteCollectionSelector()
        {
            if (null != Selector.CurrentItem)
            {
                SPWebApplication selectedWebApp = Selector.CurrentItem;
                SlaveSiteCollectionsSelector.Items.Clear();
                foreach (SPSite siteCollection in selectedWebApp.Sites)
                {
                    //The Master Site Collection can't be a slave.
                    if (MasterSiteCollectionSelector.SelectedValue != siteCollection.Url)
                    {
                        ListItem option = new ListItem();
                        SlaveSiteCollectionsSelector.Items.Add(option);
                        option.Text = siteCollection.Url;
                        option.Value = option.Text;

                        SPWeb rootWeb = siteCollection.RootWeb;
                        bool isSlave = Utilities.CheckIsSlave(rootWeb);
                        if (isSlave)
                        {
                            option.Selected = true;
                        }
                        rootWeb.Dispose();
                    }
                    siteCollection.Dispose();
                }
            }
        }
        
    }
}
